package com.example.rabbitmq6.config;

import com.example.rabbitmq6.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DLconfig {
    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).durable(true).build();
    }

    //将正常队列和死信交换机进行绑定
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                .deadLetterExchange(Constants.DL_EXCHANGE)
                .deadLetterRoutingKey("dl")
                .maxLength(10)
                .build();
    }

//    @Bean("normalQueue1")
    public Queue normalQueue1() {
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange",Constants.DL_EXCHANGE); //绑定死信交换机
        arguments.put("x-dead-letter-routing-key","dl"); //设置发送给死信交换机的routing key，死信交换机会根据这个routing key将死信传递给指定的死信队列
        return QueueBuilder.durable(Constants.NORMAL_EXCHANGE).withArguments(arguments).build();
    }

    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalQueue") Queue queue, @Qualifier("normalExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("normal");
    }

    @Bean("dlExchange")
    public DirectExchange dlExchange() {
        return ExchangeBuilder.directExchange(Constants.DL_EXCHANGE).durable(true).build();
    }

    @Bean("dlQueue")
    public Queue dlQueue() {
        return QueueBuilder.durable(Constants.DL_QUEUE).build();
    }

    @Bean("dlBinding")
    public Binding dlBinding(@Qualifier("dlQueue") Queue queue,@Qualifier("dlExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dl");
    }
}
